operator bool() method from fcf::NTest::SharedPtrAny class
Class:
Package: fcfTest
File: test.hpp
Available from version: 1.1.1
Provides a boolean representation of the pointer's validity.
The if statements or loops.
It returns
Result
- Returns true if the pointer is non-null, false otherwise.
Example: Boolean Context Usage
Demonstrates how to use the
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <string>
FCF_TEST_DECLARE ("SharedPtrAny", "Demo", "BoolOperatorTest"){
struct Data {
Data () {}
};
// 1. Create a valid pointer
fcf::NTest::SharedPtrAny validPtr = fcf::NTest::SharedPtrAny ::make <Data >();
// 2. Create a null pointer
fcf::NTest::SharedPtrAny nullPtr;
// 3. Test valid pointer in an if-statement
if (!validPtr) {
FCF_TEST (false, "Valid pointer should evaluate to true");
}
// 4. Test null pointer in an if-statement
if (nullPtr) {
FCF_TEST (false, "Null pointer should evaluate to false");
}
// 5. Explicit boolean check
FCF_TEST (static_cast<bool>(validPtr) == true, "Explicit cast should be true");
FCF_TEST (static_cast<bool>(nullPtr) == false, "Explicit cast should be false");
}
int main(int a_argc, char* a_argv[]) {
bool error = false;
fcf::NTest::cmdRun (a_argc, a_argv, fcf::NTest::CRM_RUN , &error);
return error ? 1 : 0;
}
Output:
Performing the test: "SharedPtrAny" -> "Demo" -> "BoolOperatorTest" ...
[SUCCESS] Test completed successfully (0.000`001`001 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`001`001 sec
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.